package temp;


import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.List;
import java.util.ListIterator;

import registnet.dao.CoursePeer;
import registnet.tokenizer.RegistTokenizer;


public class SectionProcessor {

	public SectionProcessor(){
		
	}
	
	public static void process(String source) throws Exception{
		FileWriter outstream = new FileWriter(System.getProperty("user.dir") + "\\course_offering.txt");
		BufferedWriter out = new BufferedWriter(outstream);
		List<String> token = RegistTokenizer.tokenize(source);
		ListIterator<String> i = token.listIterator();
		CoursePeer daoCoursePeer = new CoursePeer();
		while(i.hasNext()){
			String[] tokens = i.next().split(",");
			String[] line = new String[18];
			
			try {
				line[0] = Integer.toString(daoCoursePeer.getCourse(tokens[19]).getCourseId());
			} catch (Exception e) {
				continue;
			}
			
			line[1] = tokens[1];
			line[2] = Integer.toString((new registnet.dao.SystemPropertiesPeer()).getSystemProperties().getSemester());
			line[3] = Integer.toString((new registnet.dao.SystemPropertiesPeer()).getSystemProperties().getAcademicYear());
			line[4] = tokens[3];
			line[5] = tokens[4];
			line[6] = tokens[5];
			line[7] = tokens[6];
			line[8] = tokens[7];
			line[9] = tokens[8];
			line[10] = tokens[9];
			line[11] = tokens[10];
			line[12] = tokens[11];
			line[13] = tokens[15];
			line[14] = tokens[16];
			line[15] = tokens[17];
			line[16] = tokens[18];
			line[17] = tokens[2];
			
			try{
				StringBuffer buf = new StringBuffer(9999999);
				out.write(line[0] + "," + line[1] + "," + line[2] + "," + line[3] + "," + line[4] +","+ line[5] +","+ line[6] +","+ line[7] +","+ line[8] +","+ line[9] +","+ line[10] +","+ line[11] +","+ line[12] +","+ line[13] +","+ line[14] +","+ line[15] +","+ line[16] +","+ line[17] +"\r\n");
				String temp = line[0] + "," + line[1] + "," + line[2] + "," + line[3] + "," + line[4] +","+ line[5] +","+ line[6] +","+ line[7] +","+ line[8] +","+ line[9] +","+ line[10] +","+ line[11] +","+ line[12] +","+ line[13] +","+ line[14] +","+ line[15] +","+ line[16] +","+ line[17] +"\r\n";
					buf.append(temp+"\r\n");
				new registnet.dao.CourseOfferingPeer().importCourseOffering(buf.toString());
			}catch(Exception e){
				e.printStackTrace();
			}
		}

		out.close();
	}
	
}
